package org.yum.gen;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.font.LineBreakMeasurer;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;

public class Generator {

	private String name = "";
	private String type = "";
	private String attributes = "";
	private String actionType = "";
	private String range = "";
	private String target = "";
	private String attack = "";
	private String hit = "";
	private String effect = "";
	private boolean isEffect;
	private int width = 480;
	private int height = 150;
	private BufferedImage img;
	
	public Generator() {
	
	}
	
	public void setName(String nm) {
		name = nm;
	}
	
	public void setType(String tp) {
		type = tp;
	}
	
	public void setAttributes(String att) {
		attributes = att;
	}
	
	public void setActionType(String at) {
		actionType = at;
	}
	
	public void setRange(String rng) {
		range = rng;
	}
	
	public void setTarget(String tgt) {
		target = tgt;
	}
	
	public void setAttack(String atk) {
		attack = atk;
	}
	
	public void setHit(String ht) {
		hit = ht;
	}
	
	public void setEffect(String eft) {
		effect = eft;
	}
	
	public void setNoEffect (boolean eft) {
		isEffect = eft;
	}
	
	
	
	public BufferedImage getImage() {
		generateImg();
		return img;
	}
	
	public BufferedImage generateImg() {

		Font nameFont = new Font("Serif", Font.BOLD, 12);
		
		/*
		 * We need to adjust the card height depending on the amount of text
		 * in the effect box.
		 */
		/*
		img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g3d = (Graphics2D)img.createGraphics();
		FontMetrics nulMetrics = g3d.getFontMetrics();
		
		int y1 = 10;
		
		AttributedString attrStr1 = new AttributedString(effect);
		// Get iterator for string:
		AttributedCharacterIterator characterIterator1 = attrStr1.getIterator();

		// Get font context from graphics:
		FontRenderContext fontRenderContext1 = g3d.getFontRenderContext();

		// Create measurer:
		LineBreakMeasurer measurer1 = new LineBreakMeasurer(characterIterator1,
				fontRenderContext1);

		
		
		height += y1 / 4;
		*/
		//img = new BufferedImage(width,height, BufferedImage.TYPE_INT_ARGB);
		
		
		//***********************************************************
		
		// Maybe cheat and use the alpha channel, adding height to the image for
		// future painting. Then, when saving, only save the visible image.
		img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2d = (Graphics2D)img.createGraphics();
		
		g2d.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING,
        RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		
		FontMetrics metrics = g2d.getFontMetrics(nameFont);
		FontMetrics nullMetrics = g2d.getFontMetrics();
		
		g2d.setColor(new Color(227,232,169));
		g2d.fillRect(0, 0, width, height);
		
		g2d.setColor(Color.green);
		g2d.fillRect(0, 0, width, 25);
		
		g2d.setColor(Color.black);
		
		// Draw a black border.
		
		g2d.drawRect(0,0,width-1,height-1);
		
		
		g2d.setFont(nameFont);
		
		// Draw the name.
		g2d.drawString(name, 10, 23);
		
		g2d.setFont(null);
		
		// Draw action attributes
		g2d.drawString(attributes, width - nullMetrics.stringWidth(attributes) - nullMetrics.stringWidth(type) - 40, 23);
		
		// Draw a little symbol for fun. And to differentiate.
		
		// Get leftmost side of diamond.
		int leftside = width -  nullMetrics.stringWidth(type) - 35;
		int rightside = leftside + 10;
		int top = 11;
		int bottom = 21;
		int middle = 16;
		int xmid = leftside + 5;
		
		int[] polyCoordX = {leftside, xmid,  rightside, xmid};
		int[] polyCoordY = {middle,   top,   middle,    bottom};
		int numpoints = 4;
		Polygon p = new Polygon(polyCoordX, polyCoordY, numpoints);
		g2d.fillPolygon(p);
		
		
		
		// Draw attack type, action type, and range.
		g2d.drawString(type, width - nullMetrics.stringWidth(type) - 20, 23);
		g2d.drawString(actionType, 10, 40);
		g2d.drawString(range, width - nullMetrics.stringWidth(range) - 18 , 40);
		
		// Draw Target
		g2d.drawString("Target: ", 10, 60);
		g2d.drawString(target, nullMetrics.stringWidth("Target: ") + 20, 60);
		
		// Draw attack
		g2d.drawString("Attack: ", 10, 80);
		g2d.drawString(attack, nullMetrics.stringWidth("Attack: ") + 20, 80);
		
		// Draw Hit
		g2d.drawString("Hit: ", 10, 100);
		g2d.drawString(hit, nullMetrics.stringWidth("Hit:") + 20, 100);
		//g2d.drawString(attributes, 10, 38);
		
		if (!isEffect) {
		
		// Draw Effect
		g2d.drawString("Effect:", 10, 120);		
		//g2d.drawString(effect, nullMetrics.stringWidth("Effect:") + 20 , 120);
		
		if (effect.isEmpty())
			effect = " ";
		
		
		/*
		 *	This next part is the graphical word wrap to keep text
		 *	on the card instead of shooting off to nothingness.
		 *	This works in the horizontal direction. 
		 */
		int x = nullMetrics.stringWidth("Effect:") + 20;
		int y = 110;
		
		AttributedString attrStr = new AttributedString(effect);
		// Get iterator for string:
		AttributedCharacterIterator characterIterator = attrStr.getIterator();

		// Get font context from graphics:
		FontRenderContext fontRenderContext = g2d.getFontRenderContext();

		// Create measurer:
		LineBreakMeasurer measurer = new LineBreakMeasurer(characterIterator,
				fontRenderContext);

		while (measurer.getPosition() < characterIterator.getEndIndex()) {
			
			
			TextLayout textLayout = measurer.nextLayout(width - 60);
			y += textLayout.getAscent(); 
			textLayout.draw(g2d, x, y);

			y += textLayout.getDescent() + textLayout.getLeading(); 
			
		}
		
		
		
		g2d.dispose();
		
		}
		
		return img;
		
		

	}
	
	public void saveFile() {
			
		JFileChooser c = new JFileChooser();
		
		int rVal = c.showDialog(null, "Save");
		if (rVal == JFileChooser.APPROVE_OPTION) {
			try {
				File output = new File(c.getSelectedFile().getCanonicalPath()+".png");
				
				//ImageIcon i = new ImageIcon(genr.generateImg());
				ImageIO.write(generateImg(), "png", output);
				
			} catch (IOException w) {
		
			}
			
		}
		
		
	}
	

}
